package myLucene;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.ListIterator;
import java.util.zip.GZIPOutputStream;

public class SerializeLinkedListArray {
	public static String write(LinkedList<Integer>[] ar, String fileName) throws IOException {
		System.out.println("Serialize object " + GetTime.getTime());
		long start = System.currentTimeMillis();
		// Write to disk with FileOutputStream
		FileOutputStream f_out = new FileOutputStream(fileName);

		// Write object with ObjectOutputStream
		GZIPOutputStream out= new GZIPOutputStream(f_out);
		ObjectOutputStream obj_out = new ObjectOutputStream(out);
		// Write object out to disk
		obj_out.writeObject(ar);
		obj_out.flush();
		obj_out.close();
		System.out.println("Serialize done ...time needed "
				+ GetTime.getDuration(start, System.currentTimeMillis()));
		return fileName;
	}

	public static void read(String fileName) throws IOException, ClassNotFoundException {
		// Read from disk using FileInputStream
		System.out.println("Start");
		long start= System.currentTimeMillis();
		FileInputStream f_in = new FileInputStream(fileName);

		// Read object using ObjectInputStream
		ObjectInputStream obj_in = new ObjectInputStream(f_in);

		// Read an object
		Object obj = obj_in.readObject();

		//if (obj instanceof int[][]) {
			// Cast object to a Vector
			//int[][] ar = (int[][]) obj;
		// Do something with vector....
//		for (int i = 0; i <2; i++) {
//			for( int x =0 ; x < 3; x++){
//				System.out.println(ar[i][x]);
//			}
//		}
		if (obj instanceof LinkedList<?>[] ) {
		// Cast object to a Vector
			LinkedList<Integer>[] ar = (LinkedList<Integer>[]) obj;
			

		}
		
	System.out.println("Read object Time needed: "+GetTime.getDuration(start, System.currentTimeMillis()));
	}

}
